<template>
	<view>
		<view class="search-box">
			<uni-search-bar cancelButton='none' @input="input" :radius="100"></uni-search-bar>
		</view>
		<!-- 搜索建议列表 -->
		<view class="sugg-list" v-if="searchList.length!==0">
			<view class="sugg-item" v-for="(item,i) in searchList" :key="i"
			@click="gotoDetail(item)">
				<view class="goods_name">{{item.goods_name}}</view>
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view>
		
		<!-- 搜索历史 -->
		    <view class="history-box" v-else>
		      <!-- 标题区域 -->
		      <view class="history-title">
		        <text>搜索历史</text>
		        <uni-icons type="trash" size="17" @click="clean"></uni-icons>
		      </view>
		      <!-- 列表区域 -->
		      <view class="history-list">
		        <uni-tag size="mini" inverted="false" :text="item" v-for="(item, i) in histories" :key="i" @click="gotoGoodsList(item)"></uni-tag>
		      </view>
		    </view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer:null,
				kw:'',
				searchList:[],
				historyList:[]
			};
		},
		methods:{
			//input输入事件的处理函数
			input(e){
				clearTimeout(this.timer)
				setTimeout(()=>{
					// console.log(e)
					this.kw = e
					this.getSearchList()
				},500)
			},
			async getSearchList(){
				//判断关键词是否为空
				if(this.kw.length ===0){
					this.searchList = []
					return
				}
				//不为空，发起请求
				const {data:res} = await uni.$http.get('/api/public/v1/goods/qsearch',{query:this.kw})
				if(res.meta.status !== 200) return uni.showMsg()
				this.searchList = res.message
				
				this.getSearchHistory()
			},
			gotoDetail(item){
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
				})
			},
			//保存搜索历史记录
			getSearchHistory(){
				// this.historyList.push(this.kw)
				//1、将historyList数组转化成Set对象
				const set = new Set(this.historyList)//用historyList里的数据转化成set对象
				//2、调用Set对象的delete方法，移除对应的元素,也就是historyList中的已经存在的kw
				set.delete(this.kw)
				//3、调用add方法，向historyList中添加kw
				set.add(this.kw)
				//4、将Set对象转化为数组赋给historyList
				this.historyList = Array.from(set)
				
				//对搜索历史数据进行持久化存储
				uni.setStorageSync('kw',JSON.stringify(this.historyList))
			},
			//清空搜索历史
			clean(){
				this.historyList = []
				uni.setStorageSync('kw','[]')
			},
			gotoGoodsList(kw){
				uni.navigateTo({
					url:'/subpkg/goods_list/goods_list?query='+ kw
				})
			}
		},
		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
		},
		computed:{
			histories(){//在页面渲染时，渲染的是计算属性中的histories数组，不是原数组
				//注意：由于数组是引用类型，所以不要直接基于原数组调用reverse方法，
				// 以免修改原数组中元素的顺序
				// 而是建立一个新数组,与内存无关的数组,再进行reverse反转
				return [...this.historyList].reverse()
			}
		}
	}
</script>

<style lang="scss">
.search-box{
	position: sticky;
	top: 0;
	z-index: 999;
}
.sugg-list{
	padding: 0 10rpx;
	.sugg-item{
		font-size: 24rpx;
		padding: 26rpx 0;
		border-bottom: 1rpx solid #efefef;
		display: flex;
		align-items: center;
		justify-content: space-between;
		.goods_name{
			white-space: nowrap;
			overflow: hidden;
			text-overflow: ellipsis;
		}
	}
}
.history-box {
    padding: 0 10rpx;

    .history-title {
      display: flex;
      justify-content: space-between;
      height: 80rpx;
      align-items: center;
      font-size: 20rpx;
      border-bottom: 1rpx solid #efefef;
    }

    .history-list {
      display: flex;
      flex-wrap: wrap;

      .uni-tag {
        margin-top: 10rpx;
        margin-right: 10rpx;
      }
    }
  }
</style>
